<?php
namespace app\index\controller;
require __DIR__."/../../../qiniu/autoload.php";
use \Qiniu\Auth;

use app\index\model\appInfo as AppInfoModel;

class QiNiu
{
    public function getQiNiuDomain()
    {
        $appInfo = AppInfoModel::get(["id" => 1]);
        //返回消息
        $JsonData = controller('index/JsonData');
        $data = [
            'qiniuDomain' => $appInfo['qiniuDomain']
        ];
        $json = $JsonData->getJsonData(0, 'get qiniu domain success.', $data);
        return $json;
    }
    public function getQiNiuInfo()
    {
        $appInfo = AppInfoModel::get(["id" => 1]);
        return [
            "qiniuAccessKey" => $appInfo['qiniuAccessKey'],
            "qiniuSecretKey" => $appInfo['qiniuSecretKey'],
            "bucket" => $appInfo['qiniuBucket']
        ];
    }
    public function getQiNiuToken()
    {
        $qiniuInfo = $this->getQiNiuInfo();

        $accessKey = $qiniuInfo['qiniuAccessKey'];
        $secretKey = $qiniuInfo['qiniuSecretKey'];
        $bucket = $qiniuInfo['bucket'];
        // 初始化Auth状态
        $auth = new Auth($accessKey, $secretKey);
        // 简单上传凭证
        $expires = 3600;
        $policy = null;
        $upToken = $auth->uploadToken($bucket);
        
        $json = json_encode([
            "uptoken" => $upToken
        ]);
        return $json; 

    }
}